package vm;

import java.lang.reflect.Field;
import sun.misc.Unsafe;

/**
 * @author zzb
 * @since 2021-01-20
 */
public class DirectMemoryOOM {

  private static final int _1MB = 1024 * 1024;

  public static void main(String[] args) throws IllegalAccessException {
    Field unsafeField = Unsafe.class.getDeclaredFields()[0];
    unsafeField.setAccessible(true);
    Unsafe unsafe = (Unsafe) unsafeField.get(null);
    while (true) {
      unsafe.allocateMemory(_1MB);
    }
  }
  /**
   * Exception in thread "main" java.lang.OutOfMemoryError
   * 	at java.base/jdk.internal.misc.Unsafe.allocateMemory(Unsafe.java:616)
   * 	at jdk.unsupported/sun.misc.Unsafe.allocateMemory(Unsafe.java:462)
   * 	at vm.DirectMemoryOOM.main(DirectMemoryOOM.java:19)
   */
}
